<?php
namespace App\Repos;
use App\Models\Shop;
use App\Models\ShopWithDraw;
use App\Models\ShopEnvelope;
use App\Models\ShopUser;
use App\Models\Order;
use App\Models\Coupon;
use App\Repos\UserCouponRepo;
use App\Services\RedisService;
use Carbon\Carbon;
use DB;

class ShopRepo
{
	public static function  getList($where=[],$num=10)
	{	   
	  	$list = Shop::where($where)->paginate($num); 
	  	return $list;     
	}

	public static function get()
	{
		return  Shop::select(['id','name'])->get();
	}

	public static function create($data)
	{
		$rs['status'] = false;
		$flag = Shop::create($data);

		//默认添加一条商铺老板的信息
		$shopUser['phone']= $data['phone'];
		$shopUser['code'] = $data['license'];
		$shopUser['name'] = $data['contacter'];
		$shopUser['token']   = str_random(16);
		$shopUser['shop_id'] = $flag->id;
		$shopUser['role_id'] = 100;
		$shopUser['token_date'] = date('Y-m-d H:i:s',strtotime("+1 year"));
		$shopUser['status'] = 0;
		ShopUserRepo::create($shopUser);
		
		
		//店铺经纬度信息
		$coord = explode(',', $data['lnglat']);  
		RedisService::geoadd($coord[0],$coord[1],$flag->id);
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}

		return $rs;
	}

	public static function find($where,$column=['*'])
	{
		
		$shop = RedisService::get('fruit:shops:'.$where['id']);
		if($shop) {
			return json_decode($shop);
		} 
		$shop =  Shop::where($where)->first();
		if($shop) {
			RedisService::set('fruit:shops:'.$shop->id,$shop->toJson());	
		}
		
		return $shop;
		
	}

	/**
	 * 商户第一次登录
	 */
	public static function login($where)
	{
		return Shop::where($where)->first();
	}

	public static function findByToken($token)
	{
		
		$shop = RedisService::get('fruit:shops:'.$token);
		if($shop) {
			return json_decode($shop);
		} 
		$shop =  Shop::where(['token'->$token])->first();
		RedisService::set('fruit:shops:'.$token,$shop->toJson());
		return $shop;
		
	}

	public static function update($where,$data)
	{
		$rs['status'] = false;
		$rs['msg'] = '操作失败';
		$shop_id = $where['id'];
		try {
			//dd($where,$data);
			$flag = Shop::where($where)->update($data);

			// 删除店铺缓存
			$key = 'fruit:shops:'.$shop_id;			
			RedisService::del($key);			

			//更新店铺经纬度信息
			if(array_get($data,'lnglat')) {
				$coord = explode(',', $data['lnglat']);
				RedisService::geoadd($coord[0],$coord[1],$shop_id);
		
			}		

		} catch(\Exception $e) {
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}

		return $rs;
	}

	public static function active($where,$data)
	{
		$rs['status'] = false;
		$rs['msg'] = '用户账号异常';
		$status = 1;
		DB::beginTransaction();
		try{			
			
			$shopUser = ShopUser::where($where)->first();
			if(empty($shopUser)) {
				$rs['msg']  = '账号不存在';
				return $rs;
			}
			$shopUser->openid = $data['openid'];
			$shopUser->status = $status;
			$flag = $shopUser->save();
			
							 
			DB::commit();
		} catch(\Exception $e) {
			DB::rollBack();
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		if($flag) {
			$rs['status'] = true;
			$rs['token']  = $shopUser->token;
			return $rs;
		}
		return $rs;

		
	}

	public static function saveLogo($file)
	{
		$folder   = 'shop/';
        $ext      = $file->extension();  
        $filename = date('YmdHis').str_random(5).'.'.$ext;
        $file->storeAs('public/'.$folder, $filename);
        return '/storage/'.$folder.$filename;
	}

	/**
	 * 冻结商户，停用
	 * @param $shop_id 商户id
	 */
	public static function frozen($shop_id)
	{
		$shop = Shop::find($shop_id);
		$shop->status = '-1';
		// 店铺下所有用户账号冻结
		$data['status'] = '-1';
		ShopUser::where(['shop_id'=>$shop_id])->update($data);
		return $shop->save();
	}

	public static function  getWithdraw($shop_id,$num=10)
	{	   
	  	$list = ShopWithDraw::where(['shop_id'=>$shop_id])->paginate($num); 
	  	return $list;     
	}

	

	public static function  getEnvelope($user_id,$num=10)
	{	   
	  	$list = UserEnvelope::where(['user_id'=>$user_id])->paginate($num); 
	  	return $list;     
	}

	public static function  getOrders($shop_id,$num=10)
	{	   
	  	$list = Order::where(['shop_id'=>$shop_id])->paginate($num); 
	  	return $list;     
	}

	// 查询店铺优惠券 然后发放给用户
	public static function coupons($shop_id,$user_id)
	{
		$list = Coupon::where('shop_id',$shop_id)
						->select(['id','name','value','min_amount','valid_days'])->get();
		foreach($list as $v){
			$rs = UserCouponRepo::find(['shop_id'=>$shop_id]);
			if(!$rs) {
				$uc['shop_id']   = $shop_id;
				$uc['user_id']   = $user_id;
				$uc['coupon_id'] = $v->id;
				
				$uc['end_date'] = $end_date;
				$uc['is_used'] = 0;
				UserCouponRepo::create($uc);	
			}
			
		}				

		return $list;
	}

	public static  function check($id)
	{
		$rs['status'] = false;
		$rs['msg'] = '操作失败';
		
		try {
			
			$shop = Shop::where(['id'=>$id])->first();
			$data = json_decode($shop->update_info,true);
			$data['status'] = 1;
			Shop::where(['id'=>$id])->update($data);

			// 删除店铺缓存
			$key = 'fruit:shops:'.$shop_id;			
			RedisService::del($key);			

			//更新店铺经纬度信息
			if(array_get($data,'lnglat')) {
				$coord = explode(',', $data['lnglat']);
				RedisService::geoadd($coord[0],$coord[1],$shop_id);
		
			}		

		} catch(\Exception $e) {
			$rs['msg'] = $e->getMessage();
			return $rs;
		}
		
		
		if($flag) {
			$rs['status'] = true;
			return $rs;
		}

		return $rs;
	}

	public static function count()
	{
		$num = Shop::count();
		return $num;
	}

	

	

	

	
	
	
}